home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / rbbs_pc / mnt_201.zip / CLEANUSR.C < prev    next >
C/C++ Source or Header  |  1992-01-05  |  12KB  |  362 lines

  1. /****************************************************************************/
  2. /* CLEANUSR module for RBBSMNT v2.01, a maintenance utility for RBBS-PC     */
  3. /*╒═════════════════════════════ NOTICE ═══════════════════════════════════╕*/
  4. /*│  A limited license is granted to all users of this program to make     │*/
  5. /*│  copies if this program and distribute those copies to other users     │*/
  6. /*│  on the following three conditions:                                    │*/
  7. /*│                                                                        │*/
  8. /*│    1.   This notice is NOT altered, bypassed or removed,               │*/
  9. /*│    2.   The program is not to be distributed to others in modified     │*/
  10. /*│         form. You may make changes for your own non-commercial use     │*/
  11. /*│    3.   No fee is to be charged (or any other consideration received)  │*/
  12. /*│         for copying or distributing these programs without an express  │*/
  13. /*│         written agreement with J. Terpstra, Bamestra RBBS, PO Box 66,  │*/
  14. /*│         Beemster, The Netherlands.                                     │*/
  15. /*│                                                                        │*/
  16. /*│Copyright (C) 1991, 1992 - Jan Terpstra, Bamestra RBBS, The Netherlands.│*/
  17. /*╘════════════════════════════════════════════════════════════════════════╛*/
  18. /****************************************************************************/
  19.  
  20. #include "rbbsmnt.h"                    /* definitions for this program     */
  21. #include "externs.h"                    /* external data references         */
  22.  
  23.   /**************************************************************************/
  24.   /* clean a message file                                                   */
  25.   /**************************************************************************/
  26.  
  27. void cleanusr(void)
  28. {
  29.    long usrpos,wrkpos;
  30.    int maxrec,nlen;
  31.    int year,month,day,age;
  32.    int i,ucount,rcount,wcount,luser,xuser,kills,recs_read;
  33.    int usr,wrk;
  34.    int ch,hash_1,hash_2;
  35.    char *p,name[32];
  36.  
  37.    sprintf(logbuf, "Processing %s.", usrfile);
  38.    writelog(logbuf, 1, 3);
  39.  
  40.    if (use_wrk)
  41.    {
  42.       sprintf(wrkfile, "%s\\RBBSMNT.$$U", wkf);
  43.    }
  44.  
  45.    else
  46.    {
  47.       strcpy(wrkfile, usrfile);
  48.  
  49.       if ((p = strrchr(wrkfile, '.')) != NULL)
  50.       {
  51.          sprintf(p, ".$$U");
  52.       }
  53.  
  54.       else
  55.       {
  56.          strcat(wrkfile, ".$$U");
  57.       }
  58.    }
  59.  
  60.    /*************************************************************************/
  61.    /* open usr file                                                         */
  62.    /*************************************************************************/
  63.  
  64.  
  65.    if ((usr = open(usrfile, O_RDONLY|O_BINARY)) == ERROR)
  66.    {
  67.       sprintf(logbuf, "Error opening %s.", usrfile);
  68.       writelog(logbuf, 1, 0);
  69.       return ;
  70.    }
  71.  
  72.    /*************************************************************************/
  73.    /* open tmp file                                                         */
  74.    /*************************************************************************/
  75.  
  76.  
  77.    if (!access(wrkfile, 0))
  78.    {
  79.       unlink(wrkfile);
  80.    }
  81.  
  82.    if ((wrk = open(wrkfile, O_BINARY|O_WRONLY|O_CREAT, S_IREAD|S_IWRITE)) ==
  83.    ERROR)
  84.    {
  85.       sprintf(logbuf, "Error opening temporary USERS file.");
  86.       writelog(logbuf, 1, 0);
  87.       return ;
  88.    }
  89.    usrpos = lseek(usr, 0L, SEEK_END);
  90.    maxrec = (int)(usrpos/sizeof(RBBSUSER));
  91.  
  92.    /*************************************************************************/
  93.    /* write temporary file                                                  */
  94.    /*************************************************************************/
  95.  
  96.    printf("\r  Preparing");
  97.  
  98.    for (i = 0; i < maxrec/UBLOCK; i++)
  99.    {
  100.       write(wrk, (char *)ulist, UBLOCK *sizeof(RBBSUSER));
  101.       printf(".");
  102.    }
  103.  
  104.    for (i = 0; i < maxrec%UBLOCK; i++)
  105.    {
  106.       write(wrk, (char *)ulist, sizeof(RBBSUSER));
  107.    }
  108.    close(wrk);
  109.    wrk = open(wrkfile, O_BINARY|O_RDWR, S_IREAD|S_IWRITE);
  110.    lseek(usr, 0L, SEEK_SET);
  111.  
  112.    /*************************************************************************/
  113.    /* read userfile                                                         */
  114.    /*************************************************************************/
  115.  
  116.    recs_read = kills = luser = xuser = ucount = 0;
  117.  
  118.    while (recs_read < maxrec)
  119.    {
  120.       rcount = 0;
  121.  
  122.       if (quiet)
  123.       {
  124.          printf("\r\33[K\r  Reading ");
  125.       }
  126.  
  127.       /**********************************************************************/
  128.       /* read up to 256 user records                                        */
  129.       /**********************************************************************/
  130.  
  131.  
  132.       while (rcount < UBLOCK && recs_read < maxrec)
  133.       {
  134.          usrpos = lseek(usr, 0L, SEEK_CUR);
  135.          read(usr, (char *)ulist[rcount].user_name, sizeof(RBBSUSER));
  136.          recs_read++;
  137.  
  138.          if (ulist[rcount].user_name[0] != ' ' && ulist[rcount].user_name[0]
  139.          != '\0' && strncmp(ulist[rcount].user_name, "NEWUSER", 7))
  140.          {
  141.             ch = FALSE;
  142.  
  143.             /****************************************************************/
  144.             /* exempt user?                                                 */
  145.             /****************************************************************/
  146.  
  147.  
  148.             if (ulist[rcount].user_level >= exempt)
  149.             {
  150.                sprintf(logbuf, "%.31s exempt", ulist[rcount].user_name);
  151.                ch = TRUE;
  152.                xuser++;
  153.             }
  154.  
  155.             /****************************************************************/
  156.             /* locked user?                                                 */
  157.             /****************************************************************/
  158.  
  159.  
  160.             if (ulist[rcount].user_level <= 0)
  161.             {
  162.                sprintf(logbuf, "%.31s locked out", ulist[rcount].user_name);
  163.                writelog(logbuf, 0, 3);
  164.                ch = TRUE;
  165.                luser++;
  166.             }
  167.  
  168.             /****************************************************************/
  169.             /* check age                                                    */
  170.             /****************************************************************/
  171.  
  172.  
  173.             if (!ch)
  174.             {
  175.                sscanf(ulist[rcount].last_on, "%2u-%2u-%2u", &month, &day,
  176.                &year);
  177.                age = (int) (now-(((year-80)*365)+to_month[month-1]+day));
  178.  
  179.                if (age > max_since)
  180.                {
  181.                   sprintf(logbuf, "%.31s deleted, last on at %.8s.", ulist
  182.                   [rcount].user_name, ulist[rcount].last_on);
  183.                   writelog(logbuf, 0, 3);
  184.                   kills++;
  185.                }
  186.  
  187.                else
  188.                {
  189.                   sprintf(logbuf, "%.31s copied", ulist[rcount].user_name);
  190.                   ch = TRUE;
  191.                }
  192.             }
  193.  
  194.             /****************************************************************/
  195.             /* adjust last msg read pointer                                 */
  196.             /****************************************************************/
  197.  
  198.  
  199.             if (ch)
  200.             {
  201.  
  202.            if (do_renum && ulist[rcount].lastread)
  203.                {
  204.  
  205.           i = lives-1;
  206.           while (i && mlist[i].old_num > ulist[rcount].lastread)
  207.           {
  208.              i--;
  209.           }
  210.           ulist[rcount].lastread = mlist[i].new_num;
  211.                }
  212.  
  213.                if (quiet)
  214.                {
  215.  
  216.                   if (rcount%32 == 0)
  217.                   {
  218.                      printf(".");
  219.                   }
  220.                }
  221.                rcount++;
  222.             }
  223.  
  224.             if (!quiet)
  225.             {
  226.                printf("\r\33[K\r  %s\n", logbuf);
  227.             }
  228.          }
  229.       }
  230.  
  231.       /**********************************************************************/
  232.       /* write a block of up to 256 user records                            */
  233.       /**********************************************************************/
  234.  
  235.  
  236.       if (!quiet)
  237.       {
  238.          printf("\r\33[K");
  239.       }
  240.       printf("\r  Writing ");
  241.  
  242.       for (wcount = 0; wcount < rcount; wcount++)
  243.       {
  244.          memset(name, 0, 32);
  245.          strncpy(name, ulist[wcount].user_name, 31);/* username to workspace*/
  246.          p = name+strlen(name);
  247.  
  248.          while (isspace(*(--p)))        /* strip trailing blanks            */
  249.             ;
  250.          *(++p) = '\0';
  251.  
  252.          if (ucount%32 == 0)
  253.          {
  254.             printf("o");
  255.          }
  256.          ucount++;
  257.          nlen = strlen(name)-1;
  258.  
  259.          /*******************************************************************/
  260.          /* calculate hash values                                           */
  261.          /*******************************************************************/
  262.  
  263.          hash_1 = name[0];
  264.          hash_2 = name[nlen/2];
  265.          ch = name[nlen];
  266.          hash_1 = (int)(((hash_1 *100)+(hash_2 *10)+ch)%maxrec)+1;
  267.          ch = name[1];
  268.          hash_2 = (int)((ch *10+7)%maxrec);
  269.  
  270.          /*******************************************************************/
  271.          /* find free slot in usrfile                                       */
  272.          /*******************************************************************/
  273.  
  274.  
  275.          for (i = 0; i < maxrec; i++)
  276.          {
  277.  
  278.             if (hash_1 > 0)
  279.             {
  280.  
  281.                if (hlist[hash_1] == 'F')/* free slot?                       */
  282.                {
  283.                   wrkpos = (long)((hash_1-1)*(long)sizeof(RBBSUSER));
  284.                   lseek(wrk, wrkpos, SEEK_SET);
  285.                   write(wrk, (char *)ulist[wcount].user_name, sizeof
  286.                   (RBBSUSER));
  287.                   hlist[hash_1] = 'U';
  288.                   break;
  289.                }
  290.             }
  291.             hash_1 += hash_2;           /* get next hash value              */
  292.  
  293.             if (hash_1 > maxrec-1)      /* but not past EOF                 */
  294.             {
  295.                hash_1 -= maxrec;
  296.             }
  297.          }
  298.  
  299.          /*******************************************************************/
  300.          /* user properly saved in free slot?                               */
  301.          /*******************************************************************/
  302.  
  303.  
  304.          if (i == maxrec)               /* couldn't find free slot!!        */
  305.          {
  306.             sprintf(logbuf, "Userfile too full.");
  307.             writelog(logbuf, 1, 0);
  308.             close(usr);
  309.             close(wrk);
  310.             unlink(wrkfile);
  311.             return ;
  312.          }
  313.       }
  314.    }
  315.    close(wrk);
  316.    close(usr);
  317.  
  318.    /*************************************************************************/
  319.    /* adjust usercount in msg file header                                   */
  320.    /*************************************************************************/
  321.  
  322.  
  323.    if ((wrk = open(msgfile, O_RDWR|O_BINARY)) == ERROR)
  324.    {
  325.       sprintf(logbuf, "Error opening %s.", msgfile);
  326.       writelog(logbuf, 1, 0);
  327.       unlink(wrkfile);
  328.       return ;
  329.    }
  330.    get_rbbs_hdr(wrk);
  331.    num_users = ucount;
  332.    put_rbbs_hdr(wrk);
  333.    close(wrk);
  334.    sprintf(logbuf,
  335.    "Usrs: %05d deleted, %05d active (%04d exempt, %04d locked).", kills,
  336.    ucount, xuser, luser);
  337.    printf("\r\33[K\r  %s\n", logbuf);
  338.    writelog(logbuf, 0, 4);
  339.  
  340.    /*************************************************************************/
  341.    /* rename or copy workfile to org file                                   */
  342.    /*************************************************************************/
  343.  
  344.  
  345.    if (use_wrk)
  346.    {
  347.       sprintf(logbuf, "copy %s %s > nul", wrkfile, usrfile);
  348.       system(logbuf);
  349.       unlink(wrkfile);
  350.    }
  351.  
  352.    else
  353.    {
  354.       unlink(usrfile);
  355.       rename(wrkfile, usrfile);
  356.    }
  357. }
  358.  
  359.  
  360. /*--------------------------------------------------------------------------*/
  361.  
  362.